﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/6/6 星期四 23:37:12
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sector7.Framework.DB;
using Sector7.Medical.EntityType;
using Sector7.Medical.Consts;
using System.Data.SqlClient;

namespace Sector7.Medical.DAO
{
    /// <summary>
    /// 电子病历数据访问接口
    /// </summary>
    public class MedicalRecordDAO : DAOBase<M7_MedicalRecord>, IMedicalRecordDAO
    {
        #region Public Functions
        /// <summary>
        /// 根据人员信息编码和状态获取电子病历信息
        /// </summary>
        /// <param name="citizenId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public List<M7_MedicalRecord> ReadAll(string citizenId, int status)
        {
            string sql = @"SELECT * FROM M7_MedicalRecord mr
                         WHERE 1 = 1
                         and mr.CitizenId = @CitizenId
                         and mr.Status = @Status";

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(citizenId, "CitizenId"),
                    DBUtil.CreateSqlParameter(status, "Status"));

                List<M7_MedicalRecord> list = new List<M7_MedicalRecord>();

                while (reader.Read())
                {
                    M7_MedicalRecord medicalRecord = new M7_MedicalRecord();
                    DBUtil.ReadData(reader, medicalRecord);

                    list.Add(medicalRecord);
                }

                return list;
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="citizenId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public List<M7_MedicalRecord> ReadAll(string citizenId)
        {
            string sql = @"
                SELECT TOP 10
                    *
                FROM
                    M7_MedicalRecord mr
                WHERE
                    mr.CitizenId = @CitizenId
                ORDER BY UpdateTime DESC";

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(citizenId, "CitizenId"));

                List<M7_MedicalRecord> medicalRecordList = new List<M7_MedicalRecord>();

                while (reader.Read())
                {
                    var medicalRecord = new M7_MedicalRecord();
                    DBUtil.ReadData(reader, medicalRecord);

                    medicalRecordList.Add(medicalRecord);
                }

                return medicalRecordList;
            }
        }

        /// <summary>
        /// 根据开始日期和结束日期来查询当前病人的电子病历信息
        /// </summary>
        /// <param name="citizenId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public List<M7_MedicalRecord> ReadAll(string citizenId, DateTime startDate, DateTime endDate, string diagnostic)
        {
            string sql = @"SELECT
                            * FROM M7_MedicalRecord mr
                            WHERE 
                            1 = 1
                            and mr.CitizenId = @CitizenId
                            and DATEDIFF(DAY, mr.UpdateTime, @StartTime) <= 0 
                            and DATEDIFF(DAY, mr.UpdateTime, @EndTime) >= 0";

            using (DBHelper helper = new DBHelper())
            {
                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(citizenId, "CitizenId"),
                    DBUtil.CreateSqlParameter(startDate.ToString("yyyyMMdd"), "StartTime"),
                    DBUtil.CreateSqlParameter(endDate.ToString("yyyyMMdd"), "EndTime"));

                List<M7_MedicalRecord> medicalRecordList = new List<M7_MedicalRecord>();

                while (reader.Read())
                {
                    M7_MedicalRecord medicalRecord = new M7_MedicalRecord();
                    DBUtil.ReadData(reader, medicalRecord);
                    medicalRecordList.Add(medicalRecord);
                }

                return medicalRecordList;
            }
        }

        /// <summary>
        /// 创建电子病历信息
        /// </summary>
        /// <param name="medicalRecord"></param>
        /// <returns></returns>
        public override object Create(M7_MedicalRecord medicalRecord)
        {
            string sql = @"
                INSERT INTO M7_MedicalRecord
                        (EmrId
                        ,CitizenId
                        ,CreateTime
                        ,UpdateTime
                        ,Status
                        ,Remarks
                        ,MajorDiagnosisId
                        ,MinorDiagnosisId
                        ,DiagnosticBasisId
                        ,DiagnosticStatusId
                        ,PresentHistory
                        ,PreviousHistory
                        ,MainSuit
                        ,Process
                        ,Examination
                        ,DeptId
                        ,DoctorId)
                    VALUES
                        (@EmrId
                        ,@CitizenId
                        ,@CreateTime
                        ,@UpdateTime
                        ,@Status
                        ,@Remarks
                        ,@MajorDiagnosisId
                        ,@MinorDiagnosisId
                        ,@DiagnosticBasisId
                        ,@DiagnosticStatusId
                        ,@PresentHistory
                        ,@PreviousHistory
                        ,@MainSuit
                        ,@Process
                        ,@Examination
                        ,@DeptId
                        ,@DoctorId)";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteNonQuery(sql,
                    DBUtil.CreateSqlParameter(medicalRecord.EmrId, "EmrId"),
                    DBUtil.CreateSqlParameter(medicalRecord.CitizenId, "CitizenId"),
                    DBUtil.CreateSqlParameter(medicalRecord.CreateTime, "CreateTime"),
                    DBUtil.CreateSqlParameter(medicalRecord.UpdateTime, "UpdateTime"),
                    DBUtil.CreateSqlParameter(medicalRecord.Status, "Status"),
                    DBUtil.CreateSqlParameter(medicalRecord.Remarks, "Remarks"),
                    DBUtil.CreateSqlParameter(medicalRecord.MajorDiagnosisId, "MajorDiagnosisId"),
                    DBUtil.CreateSqlParameter(medicalRecord.MinorDiagnosisId, "MinorDiagnosisId"),
                    DBUtil.CreateSqlParameter(medicalRecord.DiagnosticBasisId, "DiagnosticBasisId"),
                    DBUtil.CreateSqlParameter(medicalRecord.DiagnosticStatusId, "DiagnosticStatusId"),
                    DBUtil.CreateSqlParameter(medicalRecord.PresentHistory, "PresentHistory"),
                    DBUtil.CreateSqlParameter(medicalRecord.PreviousHistory, "PreviousHistory"),
                    DBUtil.CreateSqlParameter(medicalRecord.MainSuit, "MainSuit"),
                    DBUtil.CreateSqlParameter(medicalRecord.Process, "Process"),
                    DBUtil.CreateSqlParameter(medicalRecord.Examination, "Examination"),
                    DBUtil.CreateSqlParameter(medicalRecord.DeptId, "DeptId"),
                    DBUtil.CreateSqlParameter(medicalRecord.DoctorId, "DoctorId"));
            }
        }
        #endregion

        #region Structure
        public MedicalRecordDAO()
        {
            m_tableName = C_M7_TableName.MEDICAL_RECORD;
            m_primaryKeyName = C_PrimaryKeyName.MEDICAL_RECORD_ID;
        }
        #endregion
    }
}
